<template>
  <div v-show="editShow">
    <div class="container">
      <el-row :gutter="20">
        <div class="clearfix">
          <div class="crumbs">
            <i class="el-icon-back"></i>
            <el-button type="text" @click="goBack">返回</el-button>
          </div>
          <div class="btn-save">
            <el-button type="primary" @click="saveClick('ruleForm')">保存</el-button>
          </div>
        </div>
        <div class="form-box">
          <el-form :model="ruleForm" ref="ruleForm" label-width="170px" class="eidt-el-form">  
            <el-form-item label="备注" prop="remark">
              <el-input v-model="ruleForm.remark"></el-input>
            </el-form-item>
            <el-form-item label="生成票数量" prop="number">
              <el-input v-model="ruleForm.number" min='1' type="number"></el-input>
            </el-form-item>
            <el-form-item label="票类型" prop="ticketTypeId">
              <el-select v-model="ruleForm.ticketTypeId" prop="ticketTypeId" placeholder="选择票类型">
                <el-option
                  v-for="(type,index) in TicketType"
                  :label="type.name"
                  :key="index"
                  :value="type.ticketTypeId">
                  <div @click="getChangeType(type)">{{type.name}}</div>
                </el-option>
              </el-select>
            </el-form-item>
           <!-- <el-form-item label="上传文件">
              <el-upload
                class="upload-demo"
                :action="action"
                :on-success="handleAvatarSuccess"
                :auto-upload="true"
                :on-change="uploadChange"
                multiple
                accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
              >
                <el-button size="small" type="primary" plain icon="el-icon-upload2">选择导入模板</el-button>
              </el-upload>
              <p class="import-text">只能选择 .xls .xlsx 文件</p>
            </el-form-item> -->
          </el-form>
        </div>
      </el-row>
    </div>
  </div>
</template>

<script>
import UploadImg from "./uploadImg/UploadImg";
import { Loading } from 'element-ui';

export default {
  data() {
    return {
      rules: {
        remark: [{ required: true, message: "备注不能为空", trigger: "blur" }],
        ticketTypeId: [{ required: true, message: "票类型不能为空", trigger: "blur" }],
        number: [{ required: true, message: "数量不能为空", trigger: "blur" }]
      }, 
      ruleForm: {
        ticketTypeId: "",
        number: "",
        typeName: "",
        projectId:localStorage.getItem('project_Id'),
        phone:localStorage.getItem("ms_username"),
        name :localStorage.getItem("name"),
        accountId:sessionStorage.getItem('accountId'),
        type: 2,
        url:""
      },
      action:localStorage.getItem("url"),
      editImg: "",
      ticketCom_Id: "",
      dataList: [],
      TicketType: [],
      fileName: {}
    };
  },
  props: {
    editShow: {
      type: Boolean,
      default: false
    },
    editData: {
      type: Object
    }
  },
  created() {
    this.getTicketType();
  },
  watch: {
    editData: function() {
      const item = this.editData;
      this.ruleForm = item;
    }
  },
  methods: {
    saveClick(ruleForm) { 
      this.$refs[ruleForm].validate(valid => {
        if (this.fileName == undefined) {
            this.$message.error("文件还未上传完成！");
            return false
        }
        if (valid) {    
          if (this.ticketCom_Id == "" || this.ticketCom_Id == undefined) {
            const loading =this.$loading({
              lock: true,
              text: 'Loading',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.7)'
            });
            this.$axios.post("/TicketRecord/", this.ruleForm).then(res => {
              if (res.data.hasError == false) {
                this.$message.success("保存成功！");
                this.goBack();
              } else {
                this.$message.error("保存失败！");
              }
              loading.close();
            });
          } else {
            this.$axios
              .put("/TicketRecord/" + this.ticketCom_Id + "", this.ruleForm)
              .then(res => {
                if (res.data.hasError == false) {
                  this.$message.success("编辑成功！");
                  this.goBack();
                  this.$refs[ruleForm].resetFields();
                } else {
                  this.$message.error("编辑失败！");
                }
              });
          }
        } else {
          return false;
        }
      });
    },
    goBack() {
      this.$emit("backFunStu");
      this.ticketCom_Id = "";
      this.$refs['ruleForm'].resetFields();
    },
    handleAvatarSuccess() {
      
    },
    uploadChange(file) {
      this.fileName = file.response;
      if (file.response != undefined ) {
        this.ruleForm.url = file.response.data
      }
    },
    getTicketType(){
      this.$axios.get('/TicketType/Project/'+this.ruleForm.projectId).then(res =>{
        if (res.data.hasError == false) {
          this.TicketType = [...res.data.data]
        }
      })
    },
    getChangeType(type){
      this.ruleForm.ticketTypeId = type.ticketTypeId;
      this.ruleForm.typeName = type.name;
    }
  },
  components: {
    UploadImg
  }
};
</script>

<style lang="scss" scope>
.el-upload {
  height: 33px !important;
  line-height: 32px !important;
  width: 130px !important;
  border: 0;

}
</style>